package com.li.core.daoBase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import com.li.core.actionBase.ActionBase;

/**
 * Data access object (DAO) for domain model
 * 
 * @author MyEclipse Persistence Tools
 */
public class DAOBase implements IDAOBase {
	protected static Log log = LogFactory.getLog(DAOBase.class);

	public JdbcTemplate getJdbcT() {
		return jdbcT;
	}

	public void setJdbcT(JdbcTemplate jdbcT) {
		this.jdbcT = jdbcT;
	}
	/**
	 * 
	 * @param sql sql 语句
	 * @param params 参数
	 * @param c  类的类型
	 * @return List 结果集
	 * @throws Exception
	 */
	public List query(String sql, Object[] params, Class c) throws Exception {
		List queryResult = new ArrayList();
		List list = this.getJdbcT().queryForList(sql, params);
		Map m = null;

		for (Object o : list) {
			m = (Map) o;
			Object bean = c.newInstance();
			for (Object obj : m.entrySet()) {
				Map.Entry<Object, Object> entry = (Map.Entry) obj;
				BeanUtils.setProperty(bean,
						((String) entry.getKey()).toLowerCase(),
						entry.getValue());
			}

			queryResult.add(bean);
		}

		return queryResult;
	}

	private JdbcTemplate jdbcT;

}